<template>
	<view class="login-container">
		<!-- 登录提示图标 -->
		<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
		<!-- 一键登录 -->
		<button type="primary" class="btn-login" @click="getUserProfile">一键登录</button>
		<!-- 登录提示 -->
		<view class="tips-text">
			登录后尽享更多权益
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	export default {
		computed: {
			...mapState('m_address',['userInfo','redirectInfo'])
		},
		name:"my-login",
		data() {
			return {
				
			};
		},
		methods: {
			...mapMutations('m_address',['updateUserInfo','updateToken','updateRedirectInfo']),
			//登录按钮
			async getUserProfile(){
				const [err,res] = await uni.getUserProfile({desc:'你的授权信息'})
				if(err == null && res.errMsg == 'getUserProfile:ok'){
					// console.log(res)
					this.updateUserInfo(res.userInfo)
					this.getToken(res)
				}else if(err.errMsg == "getUserProfile:fail auth deny"){
					uni.$showMessage("您取消了授权！")
				}
			},
			async getToken(info){
				const [err,res] = await uni.login()
				if(err || res.errMsg !== "login:ok")return uni.$showMessage("登录失败")
				// console.log(res.code)
				// console.log(info)
				// const query = {
				// 	code: res.code,
				// 	encryptedData: info.encryptedData,
				// 	rawData: info.rawData,
				// 	iv: info.iv,
				// 	signature: info.signature
					
				// }
				// const {data:loginResult} = await uni.$http.post("/api/public/v1/users/wxlogin",query)
				// console.log(loginResult)
				const token = "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo"
				uni.$showSuccess("登录成功")
				this.updateToken(token)
				this.navigateBack()
			},
			navigateBack(){
				if(this.redirectInfo!=null && this.redirectInfo.openType == 'switchTab'){
					uni.switchTab({
						url: this.redirectInfo.info,
						
					})
				}
			}
		}
	}
</script>

<style lang="scss">
.login-container{
	align-items: center;
	display: flex;
	flex-direction: column;
	height: 750rpx;
	justify-content: center;
	 // 登录按钮的样式
	.btn-login{
		width: 90%;
		border-radius: 100px;
		margin: 15px 0;
		background-color: #c00000;
	}
	 // 按钮下方提示消息的样式
	.tips-text{
		font-size: 12px;
		color: gray;
	}
}
</style>